package com.hhf.rpa.file.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhf.rpa.file.config.FileConfig;
import com.hhf.rpa.file.model.BaseFile;
import com.hhf.rpa.file.pojo.form.BaseFileForm;
import com.hhf.rpa.file.service.BaseFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
@RequestMapping("/admin/file")
public class FileAdminController {
    @Autowired
    private BaseFileService fileService;
    @Autowired
    private FileConfig config;

    @GetMapping("/imageDetail")
    public String imageDetail(@RequestParam("fileName") String fileName, HttpServletRequest request) {
        request.setAttribute("fileName",fileName);
        return "/imageDetail";
    }

    @GetMapping("/imagePage")
    public String imagePage(@ModelAttribute BaseFileForm baseFileForm, HttpServletRequest request) {
        Page<BaseFile> page = fileService.selectPage(baseFileForm);
        request.setAttribute("page",page);
        return "/imagePage";
    }

    @GetMapping("/fileUpload")
    public String fileUpload() {
        return "/fileUpload";
    }

    @PostMapping("/imageDel")
    @ResponseBody
    public String imageDel(@RequestBody BaseFileForm form, HttpServletRequest request) {
        Integer fileId = form.getFileId();
        String fileName = form.getFileName();
        fileService.deleteById(fileId);
        fileService.deleteFile(fileName, config.getStoragePath());
        return "ok";
    }
}
